package com.ruoyi.rsikManage.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.rsikManage.domain.vo.BroadCastRequestVo;
import com.ruoyi.rsikManage.service.CustomConfigurationService;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * @author wxx
 * @description  自定义配置类
 * @date 2023/11/28
 */
@RequestMapping("/CustomConfiguration")
@RestController
public class CustomConfigurationController extends BaseController {

    /**
     *  广播播报 内容
     *  播放广播内容
     */
    @Autowired
    private CustomConfigurationService customConfigurationService;

    @PostMapping("/Broadcast/Play/old")
    public TableDataInfo PlayBroadcastContent722(@RequestBody BroadCastRequestVo broadCastRequest)
    {
        customConfigurationService.PlayBroadcastContent722(broadCastRequest);
        return null;

    }

    /**
     *  广播播报 内容
     *  播放广播内容
     */
    @PostMapping("/Broadcast/Play")
    public AjaxResult PlayBroadcastContent(@RequestBody BroadCastRequestVo broadCastRequest)
    {
        AjaxResult ajaxResult = customConfigurationService.PlayBroadcastContentForUnitCodes(broadCastRequest);

        return ajaxResult;

    }



    /**
     *  广播播报 内容
     *  播放广播内容
     */

    @GetMapping("/play")
    public ResponseEntity<String> mockPlayBroadcastContent(HttpServletRequest request) {
        // 获取请求头名称的枚举
        Enumeration<String> headerNames = request.getHeaderNames();
        // 获取请求的完整 URL
        StringBuffer requestURL = request.getRequestURL();

        // 获取查询字符串
        String queryString = request.getQueryString();

        // 如果有查询字符串，则将其附加到 URL 后面
        if (queryString != null) {
            requestURL.append('?').append(queryString);
        }

        System.out.println("Request URL: " + requestURL);
        // 创建一个 LinkedHashMap 以保持请求头的顺序
        Map<String, String> headers = new LinkedHashMap<>();

        // 遍历并收集每个请求头
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            headers.put(headerName, headerValue);
            System.out.println(headerName + ": " + headerValue);
        }

        // 使用 ObjectMapper 将 Map 转换为 JSON 字符串
        ObjectMapper mapper = new ObjectMapper();
        String headersJson;
        try {
            headersJson = mapper.writeValueAsString(headers);
        } catch (Exception e) {
            // 如果转换失败，返回一个错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to serialize headers");
        }

        // 构建响应体
        Map<String, Object> responseBody = new LinkedHashMap<>();
        responseBody.put("result", true);
        responseBody.put("headers", headersJson);

        // 使用 ObjectMapper 将整个响应体转换为 JSON 字符串
        String responseBodyJson;
        try {
            responseBodyJson = mapper.writeValueAsString(responseBody);
        } catch (Exception e) {
            // 如果转换失败，返回一个错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to serialize response body");
        }

        // 返回模拟响应
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(responseBodyJson);
    }











}
